//
//  LFTabBarController.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/22.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFTabBarController.h"
#import "LFNavigationController.h"
#import "LFTabBar.h"
#import "LFComposeView.h"
#import "LFFreshOrderTableViewController.h"
#import "LPCtabBar.h"

@interface LFTabBarController ()
@property (weak, nonatomic) LPCtabBar *tab;
@end

@implementation LFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置当前的tabBar
    LFTabBar *tabBar = [LFTabBar new];
    __weak typeof(self) weakSelf =self;
    tabBar.composeBtnBlock = ^{
    LFFreshOrderTableViewController *vc =[[LFFreshOrderTableViewController alloc]init];
    LFNavigationController *navController = [[LFNavigationController alloc] initWithRootViewController:vc];
        
     NSLog(@"点击了按钮");
//        UIImage *image = [[self.view snapshotImage] applyLightEffect];
//        
//        //弹出控件器
//        LFComposeView *composeView = [[LFComposeView alloc] init];
//        composeView.backgroundColor = [UIColor colorWithPatternImage:image];
//        // 添加点击事件
//        composeView.clickBlock = ^{
//            NSLog(@"点击了按钮");
//        LFFreshOrderTableViewController *vc =[[LFFreshOrderTableViewController alloc]init];
////        LFNavigationController *navController = [[LFNavigationController alloc] initWithRootViewController:vc];
//            [self presentViewController:vc animated:YES completion:nil];
//        };
//        [composeView show:weakSelf];
    };
//    [self setValue:tabBar forKey:@"tabBar"];
    
    [self addChildViewControllers];

}
#pragma mark - 创建windows的根控制器
- (void)addChildViewControllers {
    LPCtabBar *tab = [LPCtabBar sharedTabBar];
    [self setValue:tab forKey:@"tabBar"];
    
    self.tab = tab;
    //创建字典,一次存储需要创建的tabBar内的元素
    NSArray *array = @[
                       @{@"clsName": @"LFHomeViewController", @"title": @"首页", @"imageName": @"home",@"imageNameSelect":@"v2_home_r-1"},
                       @{@"clsName": @"LFMarketViewController", @"title": @"闪电超市",@"imageName": @"v2_order-1",@"imageNameSelect":@"v2_order_r-1"},
                       @{@"clsName": @"LFFreshOrderTableViewController", @"title": @"新鲜预定",@"imageName": @"v2_my_order_icon",@"imageNameSelect":@"icon_tuikuan"},
                       @{@"clsName": @"LFOrderTableViewController", @"title": @"购物车",@"imageName": @"shopCart-1",@"imageNameSelect":@"shopCart_r-1"},
                       @{@"clsName": @"LFMineViewController", @"title": @"我的",@"imageName": @"v2_my-1",@"imageNameSelect":@"v2_my_r-1"},
                       ];
    // 遍历数组,添加子控制器
    for (NSDictionary *dict in array)
    {
        // 通过字典创建对应的控制器
        UIViewController *vc = [self viewControllerWithDict:dict];
        //将控制器添加到子控制器中
        [self addChildViewController:vc];
    }
    
    [self.tabBar setTintColor:HMColor(255, 130, 1)];
}
#pragma mark - 跟剧字典中的元素依次创建控制器
- (UIViewController *)viewControllerWithDict:(NSDictionary *)dict {
    // 1. 初始化控制器
    UIViewController *vc = [NSClassFromString(dict[@"clsName"]) new];
    // 2. 设置标题 = 导航栏标题+tabbar的标题
    vc.title = dict[@"title"];
    vc.navigationItem.title = dict[@"title"];
    //设置tabBar的颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    // 3. 设置不同状态下显示的图片
    vc.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@", dict[@"imageName"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@", dict[@"imageNameSelect"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //vc.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_h", dict[@"imageName"]]];
    //设置选中后的颜色
    //CGSize imageSize = CGSizeMake(self.tabBar.bounds.size.width*0.3333, self.tabBar.bounds.size.height);
    //self.tabBar.selectionIndicatorImage = [self drawImage:imageSize];
    //嵌套一层UINavigationController
    LFNavigationController *navController = [[LFNavigationController alloc] initWithRootViewController:vc];
    //设置标题的颜色及大小
    [navController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:18]}];
    // 返回导航控制器
    
    
    return navController;
}



@end
